const express=require("express")
const fs=require("fs");//文件读取模块
var readline = require('readline');//按行读取文件
const router=express.Router();
const path=require("path")
const read_file=require("../public/index")

const UserInfo=require("../models/userinfo")
const SignIn=require("../models/sign_in")
const ShopBuy=require("../models/shopbuy")
const Register=require("../models/register")
const Recharge=require("../models/recharge")
const NewGuide=require("../models/newguide")
const Lottery=require("../models/lottery");
const New_Guide = require("../models/newguide");

router.post("/",(req,res)=>{
   //nodejs 获取指定路径下所有的文件夹名
    let components = [];
    const file=path.join(__dirname,"../GameSrv-0");
	const files = fs.readdirSync(file)
	files.forEach(function (item, index) {
	    let stat = fs.lstatSync(file +"/"+ item)
	    if (stat.isDirectory() == true) { 
	      components.push(item)
	    }
	})
	components.forEach((item=>{
		// console.log("./GameSrv-0"+"/"+item);
        //匹配指定路径下的所有文件
        const file=path.join(__dirname,"../GameSrv-0");
		const files = fs.readdirSync(file+"/"+item)
		files.forEach(function (filess, index) {
		    let stat = fs.lstatSync(file+"/"+item+"/" + filess)
		    if (stat.isFile() == true) { 
			  var result=filess.toString().match(/.INFO./g)
			  if(result==null){
				  return;
			  }else{
				  var directory=file+"/"+item+"/"+filess
				  read_file(directory,function (data) {
				       data.forEach(item=>{
						   var result=item.toString().match("game_oss_log")
						   if(result==null){
							   return;
						   }else{
							   // console.log(item.toString().split("|"))
							   if(item.toString().split("|")[1]==2){
								   var obj={
								         channel_id:"",
								         count:item.toString().split("|")[5],
								         date:item.toString().split("|")[3],
								         dollar:item.toString().split("|")[8],
								         gift_id:"",
								         recharge_id:item.toString().split("|")[7],
								         server_id:"",
								         uid:item.toString().split("|")[4]
								     }
								     new Recharge(obj).save()
							   }
							   if(item.toString().split("|")[1]==3){
								   var obj={
								       channel_id:"",
								       count:"",
								       date:item.toString().split("|")[3],
								       event_id:"",
								       group_id:"",
								       server_id:"",
								       uid:item.toString().split("|")[4],
								       expire:""
								   }
								   new New_Guide(obj).save()
							   }
							   if(item.toString().split("|")[1]==11){
								   var obj={
								       channel_id:"",
								       count:item.toString().split("|")[5],
								       date:item.toString().split("|")[3],
								       lottery_id:"",
								       lottery_num:"",
								       server_id:"",
								       uid:item.toString().split("|")[4],
								       expire:""
								   }
								   new Lottery(obj).save()
							   }
							  if(item.toString().split("|")[1]==12){
								  console.log(item.toString().split("|"))
							  }
						   }
					   })
				  });
			  }
		    }
		})
	}));
})



router.post("/userinfo",(req,res)=>{
    const obj={
        account:req.body.account,
        afkcheckpoint:req.body.afkcheckpoint,
        channel_id:req.body.channel_id,
        diamond:req.body.diamond,
        gold:req.body.gold,
        login_time:req.body.login_time,
        logout_time:req.body.logout_time,
        nickname:req.body.nickname,
        power_value:req.body.power_value,
        reg_time:req.body.reg_time,
        server_id:req.body.server_id,
        role_format:role_format,
        total_recharge:req.body.total_recharge,
        uid:req.body.uid,
        user_ip:req.body.user_ip,
        user_level:req.body.user_level,
        vip_level:req.body.vip_level
    }
    new UserInfo(obj).save().then(userinfo=>{
        res.send({
            code:200,
            msg:"success",
            data:userinfo
        })
    })
})


router.post("/signin",(req,res)=>{
    var obj={
        register_time:req.body.register_time,
        sign_in_date:req.body.sign_in_date,
        uid:req.body.uid
    }
    new SignIn(obj).save().then(signin=>{
        res.send({
            code:200,
            msg:"success",
            data:signin
        })
    })
})


router.post("/shopbuy",(req,res)=>{
    var obj={
        channel_id:req.body.channel_id,
        count:req.body.count,
        date:req.body.date,
        money_cost:req.body.money_cost,
        money_type:req.body.money_type,
        product_id:req.body.product_id,
        server_id:req.body.server_id,
        shop_id:req.body.shop_id,
        shop_type:req.body.shop_type,
        uid:req.body.uid,
        expire:req.body.expire
    }
    new ShopBuy(obj).save().then(shopbuy=>{
        res.send({
            code:200,
            msg:"success",
            data:shopbuy
        })
    })
})


router.post("/register",(req,res)=>{
    var obj={
        register_time:req.body.register_time,
        uid:req.body.uid
    }
    new Register(obj).save().then(register=>{
        res.send({
            code:200,
            msg:"success",
            data:register
        })
    })
})


router.post("/recharge",(req,res)=>{
    var obj={
        channel_id:req.body.channel_id,
        count:req.body.count,
        date:req.body.date,
        dollar:req.body.dollar,
        gift_id:req.body.gift_id,
        recharge_id:req.body.recharge_id,
        server_id:req.body.server_id,
        uid:req.body.uid
    }
    new Recharge(obj).save().then(recharge=>{
        res.send({
            code:200,
            msg:"success",
            data:recharge
        })
    })
})


router.post("/newguide",(req,res)=>{
    var obj={
        channel_id:req.body.channel_id,
        count:req.body.count,
        date:req.body.date,
        event_id:req.body.money_cost,
        group_id:req.body.money_type,
        server_id:req.body.server_id,
        uid:req.body.uid,
        expire:req.body.expire
    }
    new New_Guide(obj).save().then(newguide=>{
        res.send({
            code:200,
            msg:"success",
            data:newguide
        })
    })
})


router.post("/lottery",(req,res)=>{
    var obj={
        channel_id:req.body.channel_id,
        count:req.body.count,
        date:req.body.date,
        lottery_id:req.body.money_cost,
        lottery_num:req.body.money_type,
        server_id:req.body.server_id,
        uid:req.body.uid,
        expire:req.body.expire
    }
    new Lottery(obj).save().then(lottery=>{
        res.send({
            code:200,
            msg:"success",
            data:lottery
        })
    })
})


module.exports=router